package com.ltkj.shop.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ltkj.shop.domain.vo.ShopPostageConfigVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.shop.domain.ShopPostageConfig;
import com.ltkj.shop.service.IShopPostageConfigService;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.utils.poi.ExcelUtil;

/**
 * 邮费配置Controller
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Api(tags = "邮费配置")
@RestController
@RequestMapping("/postage")
public class ShopPostageConfigController extends BaseController
{
    @Autowired
    private IShopPostageConfigService shopPostageConfigService;

    /**
     * 查询邮费配置列表
     */
    @ApiOperation("查询邮费配置列表")
    @RequiresPermissions("shop:postage:list")
    @GetMapping("/list")
    public AjaxResult list(ShopPostageConfig shopPostageConfig)
    {
        List<ShopPostageConfig> list = shopPostageConfigService.selectShopPostageConfigList(shopPostageConfig);
        return success(list);
    }

    /**
     * 根据收货地址编码精确获取运费
     */
    @ApiOperation("根据收货地址编码精确获取运费")
    @GetMapping(value = "/getOneByCode")
    public AjaxResult getOneByCode(ShopPostageConfigVo shopPostageConfigVo)
    {
        ShopPostageConfig config = shopPostageConfigService.selectShopPostageConfigByCode(shopPostageConfigVo);
        if (null != config) {
            return success(config);
        } else {
            return error("不存在当前地址的运费配置！请联系管理员进行设置！");
        }
    }

    /**
     * 导出邮费配置列表
     */
    @ApiOperation("导出邮费配置列表")
    @RequiresPermissions("shop:postage:export")
    @Log(title = "邮费配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShopPostageConfig shopPostageConfig)
    {
        List<ShopPostageConfig> list = shopPostageConfigService.selectShopPostageConfigList(shopPostageConfig);
        ExcelUtil<ShopPostageConfig> util = new ExcelUtil<ShopPostageConfig>(ShopPostageConfig.class);
        util.exportExcel(response, list, "邮费配置数据");
    }

    /**
     * 获取邮费配置详细信息
     */
    @ApiOperation("获取邮费配置详细信息")
    @RequiresPermissions("shop:postage:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(shopPostageConfigService.selectShopPostageConfigById(id));
    }

    /**
     * 新增邮费配置
     */
    @ApiOperation("新增邮费配置")
    @RequiresPermissions("shop:postage:add")
    @Log(title = "邮费配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShopPostageConfig shopPostageConfig)
    {
        ShopPostageConfigVo vo = new ShopPostageConfigVo();
        vo.setCounty(shopPostageConfig.getRegionCode());
        if (null != shopPostageConfigService.selectShopPostageConfigByCode(vo))
        {
            return error("新增 '" + shopPostageConfig.getRegionName() + "' 运费失败！运费已存在");
        }
        return toAjax(shopPostageConfigService.insertShopPostageConfig(shopPostageConfig));
    }

    /**
     * 修改邮费配置
     */
    @ApiOperation("修改邮费配置")
    @RequiresPermissions("shop:postage:edit")
    @Log(title = "邮费配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShopPostageConfig shopPostageConfig)
    {
        return toAjax(shopPostageConfigService.updateShopPostageConfig(shopPostageConfig));
    }

    /**
     * 删除邮费配置
     */
    @ApiOperation("删除邮费配置")
    @RequiresPermissions("shop:postage:remove")
    @Log(title = "邮费配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(shopPostageConfigService.deleteShopPostageConfigByIds(ids));
    }
}
